import React, { Component } from 'react';
import { connect } from 'dva';
import { Modal, Form, Input, InputNumber, TreeSelect } from 'antd';
const TreeNode = TreeSelect.TreeNode;
const FormItem = Form.Item;

class AssetGroupModal extends Component {
  constructor(props) {
    super(props);
    this.state = {
      visible: false,
    };
  }

  okHandler = () => {
    const { onOk } = this.props;
    this.props.form.validateFields((err, values) => {
      if (!err) {
        onOk(values);
      }
    });
  };

  render() {
    const { children, assetType, item ,title,assetTypeId } = this.props;
    const { getFieldDecorator } = this.props.form;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };

    const typeTreeData = (i) => {
      return i.map( d => {
        d.label = d.name;
        d.key = d.id_+'';
        d.value = d.id_+'';
        d.children = d.assetType && d.assetType.length > 0 ? typeTreeData(d.assetType) : [];
        return d;
      })
    }

    return (
        <span>
            <span onClick={this.showModelHandler}>
                { children }
            </span>
            <Modal
                maskClosable= {this.props.maskClosable}
                confirmLoading= {this.props.confirmLoading}
                title={this.props.title}
                visible={this.props.visible}
                onOk={this.okHandler}
                onCancel={this.props.onCancel}
                key={this.props.newKey}
            >
              <Form layout='horizontal' onSubmit={this.okHandler}>
                 <FormItem style={{marginBottom:0}}>
                  {getFieldDecorator('id',{initialValue:item.id_})(
                    <Input type="hidden"/>
                  )}
                </FormItem>

                <FormItem
                  {...formItemLayout}
                  hasFeedback
                  label="属性组名称"
                >
                  {
                    getFieldDecorator('name', {
                     initialValue: item.name,
                      rules: [{
                        required: true, whitespace: true, message: '请输入属性组名称',
                      }]
                    })(<Input />)
                  }
                </FormItem>
                <FormItem
                  {...formItemLayout}
                  hasFeedback
                  label="资产分类"
                >
                  {
                    getFieldDecorator('assetTypeId', {
                      initialValue: assetTypeId ? assetTypeId + '':undefined ,
                      rules: [{
                        required: true, message: '请选择资产分类',
                      }]
                    })(
                      <TreeSelect
                        showSearch
                        treeNodeFilterProp="title"
                        dropdownStyle={{ maxHeight: 400, overflow: 'auto' }}
                        notFoundContent="无匹配结果"
                        placeholder="请选择资产分类"
                        allowClear
                        treeData={typeTreeData(assetType)}
                        treeDefaultExpandAll
                      >
                      </TreeSelect>
                    )
                  }
                </FormItem>
              
              </Form>
            </Modal> 
        </span>
    );
  }
}

export default Form.create()(AssetGroupModal);